how to deploy mongodb on kubernetes1.20

  1. clone git repository

    git clone https://github.com/mongodb/mongodb-kubernetes-operator.git
    
  2. install mongodb

    kubectl apply -f config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml
    kubectl apply -k config/rbac/ --namespace chenshi
    kubectl create -f config/manager/manager.yaml --namespace chenshi
    

    edit config/samples/mongodb.com_v1_mongodbcommunity_cr.yaml,replace version、security、password,this is mine:

    --
    apiVersion: mongodbcommunity.mongodb.com/v1
    kind: MongoDBCommunity
    metadata:
      name: mongodb
    spec:
      members: 1
      type: ReplicaSet
      version: "4.2.6"
      security:
        authentication:
          modes: ["SCRAM-SHA-1"]
      users:
        - name: admin
          db: admin
          passwordSecretRef: # a reference to the secret that will be used to generate the user's password
            name: my-user-password
          roles:
            - name: clusterAdmin
              db: admin
            - name: userAdminAnyDatabase
              db: admin
          scramCredentialsSecretName: my-scram
      additionalMongodConfig:
        storage.wiredTiger.engineConfig.journalCompressor: zlib
    
    # the user credentials will be generated from this secret
    # once the credentials are generated, this secret is no longer required
    ---
    apiVersion: v1
    kind: Secret
    metadata:
      name: my-user-password
    type: Opaque
    stringData:
      password: chenshi.net
    
  3. create db and user

    use admin
    db.auth("admin","chenshi.net")
    use chenshi
    db.createUser({user:"chenshi",pwd:"chenshi.net",roles:[{role:"readWrite",db:"chenshi"}],mechanisms:["SCRAM-SHA-1"]})
    
  4. backup and restore

    mongodump -u chenshi -p chenshi.net -d chenshi -o chenshi/
    mongorestore -u chenshi -p chenshi.net -d chenshi chenshi/
    

references: